package com.edu.dao.impl;

import com.edu.dao.CommentDao;
import com.edu.dao.BaseDao;
import com.edu.entity.Comment;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

public class CommentDaoImp extends BaseDao implements CommentDao {
    @Override
    public List<Comment> querCommentByCarId(int carId) {
        String sql = "SELECT comments.* , users.`name`  from comments INNER JOIN  users ON users.id = comments.user_id WHERE  vehicle_id =  " + carId;
        System.out.println(sql);
        ResultSet resultSet = super.queryResultSet(sql);
        List<Comment> list = new ArrayList<>();
        try {
            while (resultSet.next()) {
                int id = resultSet.getInt("id");
                String name = resultSet.getString("name");
                int vehicleId = resultSet.getInt("vehicle_id");
                String content = resultSet.getString("content");
                int rating = resultSet.getInt("rating");
                LocalDateTime commentTime = resultSet.getTimestamp("comment_time").toLocalDateTime();
              list.add( new Comment(id,name,vehicleId,content,rating,commentTime));
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            super.closeAll(conn, ps, resultSet);
        }
        return list;
    }
}

